package lsf.itcast.dao;

import lsf.itcast.domain.Permission;
import lsf.itcast.domain.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.List;

public interface RoleDao {

    //根据用户表和角色表，查询角色和用户对应关系
    @Select("select * from role where id in (select roleId from users_role where userId=#{userId})")
    @Results({@Result(id = true,property ="id" ,column = "id"),
            @Result(property ="roleName" ,column = "roleName"),
            @Result(property ="roleDesc" ,column = "roleDesc"),
            @Result(property ="permissions" ,column = "id",javaType = List.class,
            many = @Many(select = "lsf.itcast.dao.PermissionDao.findByRoleId"))

    })
    List<Role> findRoleById(String userId);

    //查询所有角色
    @Select("select * from role")
    List<Role> findAll();

    //新增用户
    @Insert("insert into role(roleName,roleDesc) values(#{roleName},#{roleDesc})")
    void save(Role role);

    //根据用户id查询角色信息，排除在中间表中只存在角色id，不存在用户id
    @Select("select * from role where id not in (select roleId from users_role where userId=#{userId}) ")
    List<Role> findOtherRole(String userId);

    //根据roleId查询角色信息
    @Select("select * from role where id=#{roleId} ")
    Role findById(String roleId);

    //给角色添加权限
    @Insert(" insert into role_permission(roleId,permissionId) values(#{roleId},#{permissionId})")
    //注意：@@Param("permissionId")注解中的名称必须与参数名称一致，否则无法成功
    void addPermissionToRole(@Param("roleId") String roleId,@Param("permissionId") String permissionId);
}
